package com.example.dllo.adapter;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.example.dllo.interface_.OnMyItemClick;
import com.example.dllo.entity.AnswerList;
import com.example.dllo.farm_doctor.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AnswerAdapter extends RecyclerView.Adapter<AnswerAdapter.MyViewHolder> {
    private List<AnswerList> answerLists;
    private RequestQueue queue;
    private OnMyItemClick onMyItemClick;

    public void setOnMyItemClick(OnMyItemClick ol) {

        onMyItemClick = ol;

    }

    public AnswerAdapter(Context c) {
        queue = Volley.newRequestQueue(c);
    }

    public void addData(List<AnswerList> datas) {
        answerLists = datas;
        notifyDataSetChanged();

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.fill_answer, null);

        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (answerLists != null && answerLists.size() > 0) {
            AnswerList entity = answerLists.get(position);
            holder.textContext.setText(entity.getContent());
           List<String> url = entity.getPicUrls();
            for (int i = 0; i < url.size(); i++){
                getBitmap(url.get(i), holder.imageContext);
                Log.i("hjhjhjk" , url.get(i));
            }
            holder.itemposition = position;
        }

    }

    private void getBitmap(String url, final ImageView imageTitle) {
        ImageRequest request = new ImageRequest(url,
                new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                imageTitle.setImageBitmap(bitmap);

            }
        }, 300, 300, Config.ARGB_8888, new Response.ErrorListener() {


            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();
            }
        }){
            @Override
            public Map<String, String>getHeaders() throws AuthFailureError{
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("Accept-Encoding","");
                return map;

            }
        };
        queue.add(request);
    }

    @Override
    public int getItemCount() {
        return answerLists != null && answerLists.size() > 0 ? answerLists.size() : 0;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView imageTitle;
        private TextView textTitle;
        private LinearLayout linearLayout;
        private int itemposition;
        private ImageView imageContext;
        private TextView textContext;


        public MyViewHolder(View itemView) {
            super(itemView);

//            textTitle = (TextView) itemView.findViewById(R.id.textTitle);
//            imageTitle = (ImageView) itemView.findViewById(R.id.imageTitle);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
            imageContext = (ImageView) itemView.findViewById(R.id.imageContext);
            textContext = (TextView) itemView.findViewById(R.id.textContext);

        }

        @Override
        public void onClick(View v) {
            if (answerLists != null) {
                AnswerList entity = answerLists.get(itemposition);
                if (onMyItemClick != null) {
                    onMyItemClick.onMyItemClick(entity);
                }

            }

        }
    }


}
